/**
 *
 */
package com.lbiuse.cwc.session;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author jss
 *
 */
public class UserOnLineFilter extends HttpServlet implements Filter {

	private static final Logger logger = Logger
			.getLogger(UserOnLineFilter.class);

	private static final long serialVersionUID = 1L;
	private boolean sessionCreate = false;
	private boolean bRoot = false;
	private int logIdType = 0; //0: none, 1:logSession, 2:logUserId, 3: both
	private int postLogLen = -1; //-1: none, 0: no limit, >=1: limit
	private boolean logResponse = false;

	private NoAuthXML noAuthXML = new NoAuthXML();

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		bRoot = Boolean.parseBoolean(filterConfig.getInitParameter("root"));
		sessionCreate = Boolean.parseBoolean(filterConfig
				.getInitParameter("sessionCreate"));
		logResponse = Boolean.parseBoolean(filterConfig
				.getInitParameter("logResponse"));
		String strLogIdType = filterConfig.getInitParameter("logIdType");
		if (StringUtils.isNumeric(strLogIdType)) {
			logIdType = Integer.parseInt(strLogIdType);
		}
		String strPostLogLen = filterConfig.getInitParameter("postLogLen");
		if (StringUtils.isNumeric(strPostLogLen)) {
			postLogLen = Integer.parseInt(strPostLogLen);
		}
		if (logger.isDebugEnabled()) {
			StringBuilder sb = new StringBuilder();
			sb.append("sessionCreate:");
			sb.append(sessionCreate);
			sb.append(" --logIdType:");
			sb.append(logIdType);
			sb.append(" --bRoot:");
			sb.append(bRoot);
			sb.append(" --postLogLen:");
			sb.append(postLogLen);
			sb.append(" --logResponse:");
			sb.append(logResponse);
			logger.debug(sb.toString());
		}
		noAuthXML.init(filterConfig.getInitParameter("noAuthXml"));
	}

	/**
	 * 过滤机制： 1.取格式化的uri：去除服务名称，首字段反斜杠； 2. 3.验证是否root节点放行
	 */
	@Override
	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;


		filterChain.doFilter(request, response);


	}

}